package cn.schoolwow.util.module.compare.jsonarray.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.compare.jsonarray.UpdateJSONArrayCompare;
import cn.schoolwow.util.domain.query.jsonobject.QueryJSONObject;
import cn.schoolwow.util.module.compare.jsonarray.domain.CompareJSONArrayQueryOption;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SetAddAndUpdateCompareJSONArrayFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        CompareJSONArrayQueryOption compareJSONArrayQueryOption = (CompareJSONArrayQueryOption) flowContext.checkData("compareJSONArrayQueryOption");

        JSONArray addCompareArray = new JSONArray();
        List<UpdateJSONArrayCompare> updateJSONArrayCompareList = new ArrayList();
        for(int i = 0; i<compareJSONArrayQueryOption.afterJSONArray.size(); i++){
            JSONObject afterInstance = compareJSONArrayQueryOption.afterJSONArray.getJSONObject(i);

            Object afterAddCheckUniqueValue = QueryJSONObject.newQuery(afterInstance)
                    .execute()
                    .getMultipleFieldValue(compareJSONArrayQueryOption.addCheckFieldNames);

            Object afterUpdateCheckUniqueValue = QueryJSONObject.newQuery(afterInstance)
                    .execute()
                    .getMultipleFieldValue(compareJSONArrayQueryOption.updateCheckFieldNames);

            boolean exist = false;
            for(int j = 0; j<compareJSONArrayQueryOption.beforeJSONArray.size(); j++){
                JSONObject beforeInstance = compareJSONArrayQueryOption.beforeJSONArray.getJSONObject(j);

                Object beforeAddCheckUniqueValue = QueryJSONObject.newQuery(beforeInstance)
                        .execute()
                        .getMultipleFieldValue(compareJSONArrayQueryOption.addCheckFieldNames);

                Object beforeUpdateCheckUniqueValue = QueryJSONObject.newQuery(beforeInstance)
                        .execute()
                        .getMultipleFieldValue(compareJSONArrayQueryOption.updateCheckFieldNames);

                if(beforeAddCheckUniqueValue.equals(afterAddCheckUniqueValue)){
                    //已存在,判断是否有更新
                    if(!beforeUpdateCheckUniqueValue.equals(afterUpdateCheckUniqueValue)){
                        UpdateJSONArrayCompare updateJSONArrayCompare = new UpdateJSONArrayCompare();
                        updateJSONArrayCompare.beforeObject = beforeInstance;
                        updateJSONArrayCompare.afterObject = afterInstance;
                        updateJSONArrayCompareList.add(updateJSONArrayCompare);
                    }
                    exist = true;
                }
            }
            if(!exist){
                //新增
                addCompareArray.add(afterInstance);
            }
        }

        flowContext.putTemporaryData("addCompareArray", addCompareArray);
        flowContext.putTemporaryData("updateJSONArrayCompareList", updateJSONArrayCompareList);
    }

    @Override
    public String name() {
        return "设置新增和更新比较JSON列表";
    }
}
